//index.js
wx.cloud.init()
const db = wx.cloud.database();
const posterCollection = db.collection("poster");
const db2 = require('../../assets/db/db.js');
const { date, todayOne, index_sort,fjRecommend } = require('../../config/sorts');
Page({
  data: {
    list:db2.list,
    value: '',
    results: [],
    activeTabIndex:0,
    activeSwioerIndex:'0',
    fjRecommend_fj : [],
    fjRecommend_gc : [],
    activeNavIndex : 0,
    date,
    todayOne : [],
    index_sort : []
  },
  onSearch(e) {
    wx.showToast({
      title: '查询中',
      icon: 'loading'
    })
    let keyword = e.detail;
    // console.log(keyword);
    if (!keyword.trim()) {
      wx.showToast({
        title: '请输入要搜索的关键字',
        icon: 'error'
      })
      return ;
    }

    posterCollection
      .where({
        title: db.RegExp({
          regexp: keyword,
          options: 'i'
        })
      })
      .orderBy('ts', 'asc')
      .get()
      .then(res => {
        console.log(res)
        this.setData({
          results: res.data
        })
      })

  },
  onCancel() {
    this.setData({
      results: []
    })
  },
  switchTab(e){
    // console.log(e);
    let index = e.currentTarget.dataset.index;
    if(index == this.data.activeTabIndex){
      return ;
    }
    this.setData({
      activeTabIndex:index,
      activeSwioerIndex:index
    })
  },
  doListChange(e){
   // console.log(e);
   let index = e.detail.current;
   this.setData({
     activeTabIndex:index
   })

  },
  findMore(e) {
    wx.navigateTo({
      url: './index_more/index_more',
    })
  },
  changeDate(e) {
    let {index} = e.currentTarget.dataset;
    // let {index, owntype} = e.currentTarget.dataset;
    // let specific1 = specific.filter((spec1) => spec1.stype == owntype);
    this.setData({
      activeNavIndex: index,
    })
  },
  doDateContentChange(e) {
    let index = e.detail.current;
    let todayOne1 = todayOne.filter((todayone) => todayone.d_id == (index+1))
    this.setData({
      activeNavIndex: index,
      todayOne : todayOne1
    })
  },
  changeAndChange1(e) {
    let i = 0;
    let set = new Set();
    let fjRecommend_fj1 = [];
    let fjRecommend_fj = fjRecommend.filter((fj) => fj.type == 'fj');
    while(true) {
      let j = Math.floor(Math.random() * fjRecommend_fj.length);
      set.add(j);
      if(set.size == 3) break;
    }
    for(var s of set){
      fjRecommend_fj1[i] = fjRecommend_fj[s];
      ++ i;
      if(i == 3) break;
    }
    console.log('----');
    fjRecommend_fj = fjRecommend_fj1.slice(0,3);
    this.setData({
      fjRecommend_fj,
    })
  },
  changeAndChange2(e) {
    let i = 0;
    let set = new Set();
    let fjRecommend_gc1 = [];
    let fjRecommend_gc = fjRecommend.filter((gc) => gc.type == 'gc');
    while(true) {
      let j = Math.floor(Math.random() * fjRecommend_gc.length);
      set.add(j);
      console.log(j,'-----');
      if(set.size == 3) break;
    }
    for(var s of set){
      fjRecommend_gc1[i] = fjRecommend_gc[s];
      ++ i;
      if(i == 3) break;
    }
    fjRecommend_gc = fjRecommend_gc1.slice(0,3);
    this.setData({
      fjRecommend_gc,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let todayOne1 = todayOne.filter((todayone) => todayone.d_id == 1);
    let fjRecommend_fj = fjRecommend.filter((fj) => fj.type == 'fj').slice(0,3);
    let fjRecommend_gc = fjRecommend.filter((gc) => gc.type == 'gc').slice(0,3);
    let index_sort1 = index_sort.filter((is) => is.type == 'mstyle').slice(0,6);
    this.setData({
      todayOne : todayOne1,
      fjRecommend_fj,
      fjRecommend_gc,
      index_sort : index_sort1
    })
  },
  loadData(){
    if (this.data.activeTabIndex == 0) {
      this.loadHot();
    } 
    else {
      this.loadChase();
    }
  },
  loadHot(){

  },
  loadChase(){

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
